import React, { useEffect } from 'react'
import Navbar from '@/comopnents/Navbar'
import { Button } from 'antd-mobile'
import { useLocation, useNavigate, useSearchParams } from 'react-router-dom'
import styles from './index.module.less'
import axios from 'axios'
import qs from 'qs'
export default function PayResult() {
    const navigate = useNavigate()
    // console.log(  useLocation().search);
    // 获取传递来的价格  
    const [searchParams] = useSearchParams()  
    let price = searchParams.get('price') as string

    let type=searchParams.get('type') as string
    let out_trade=searchParams.get('out_trade_no') as string
    let trade=searchParams.get('trade_no') as string 
    let totalAmount=searchParams.get('total_amount') as string
    console.log(totalAmount);
    
    let data = {
        out_trade_no: out_trade,
        trade_no: trade
    }
    const payResult = () => {
        axios({
            url: 'http://localhost:3016/pay/payresult',
            method: "post",
            data: qs.stringify(data)
        }).then(res => {
            console.log(res);
        })
    }
    useEffect(() => {
        payResult()
    }, [])
   

    // 回到个人中心
    const gotoMy = () => {
        navigate(`/my`, { replace: true })
    }
    return (
        <div>
            <Navbar title='乐居'></Navbar>
            <div className={styles.resultBox}>
                <div className={styles.title}>订单支付成功</div>
                { type==='1'?<div>支付金额:{totalAmount}元</div>: <div>支付金额:{price}元</div>
                }
               
            </div>
            <div className={styles.btn}>
                <Button color='primary' fill='outline' onClick={gotoMy}>个人中心</Button>
            </div>
        </div>
    )
}
